package com.css.apps.base.org.action;

import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.css.apps.base.log.model.LogConstant;
import com.css.apps.base.log.model.LogPart;
import com.css.apps.base.org.model.SOrg;
import com.css.core.configuration.Environment;
import com.css.core.model.tree.Tree;
import com.css.core.model.tree.TreeCache;
import com.css.core.model.tree.TreeNode;
import com.css.db.query.QueryCache;
import com.css.db.query.TransactionCache;
import com.css.util.Ajax;
import com.css.util.ListUtil;
import com.css.util.Messages;
import com.css.util.StringHelper;
import com.css.webbase.webwork.action.AdminAction;
import com.opensymphony.xwork.Action;
import com.opensymphony.xwork.ModelDriven;

@SuppressWarnings("serial")
public class UpdOrg extends AdminAction implements ModelDriven {
	
	private static Log log = LogFactory.getLog(UpdOrg.class);
	private SOrg item;

	public UpdOrg() {
		this.item = new SOrg();
	}

	@Override
	protected String adminGo() {
		TransactionCache tx = null;
		try {
			Tree tree = new TreeCache(SOrg.class);
			TreeNode rootNode = null;
			if (tree != null) {
				List<TreeNode> roots = tree.getRoot();
				if (ListUtil.isLstNotEmpty(roots)) {
					rootNode = roots.get(0);
				}
			}
			if (StringHelper.isEmpty(item.getUuid()) || StringHelper.isEmpty(item.getName())
					|| StringHelper.isEmpty(item.getOpenFlag())) {
				result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.fieldEmpty"));
				return Action.ERROR;
			}
			SOrg oldItem = QueryCache.get(SOrg.class, item.getUuid());
			if (oldItem == null) {
				result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.recordnotfound"));
				return Action.ERROR;
			}
			List<TreeNode> allorglist = rootNode.getAllChildren();
			for (TreeNode org : allorglist) {
				SOrg tmp = QueryCache.get(SOrg.class, org.getNodeId());
				// 所有机构的内部机构码和单位编码都不能相同
				if (item.getName().equals(tmp.getName()) && (!item.getOrgId().equals(tmp.getOrgId()))) {
					TreeNode nodetmp = tree.getTreeNode(tmp.getUuid());
					TreeNode nodeold = tree.getTreeNode(item.getUuid());
					if (nodetmp.getParent() != null && nodeold.getParent() != null
							&& nodetmp.getParentId().equals(nodeold.getParentId())) {
						result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR,
								Messages.getString("systemMsg.nameExist"));
						return Action.ERROR;
					}
				}
			}
			if (checkUpdate(oldItem, item)) {
				oldItem.setOrderNum(item.getOrderNum());
				oldItem.setCode(item.getCode());
				oldItem.setName(item.getName());
				oldItem.setOpenFlag(item.getOpenFlag());
				oldItem.setOrgType(item.getOrgType());
				oldItem.setEditDate(new Date());
				tx = new TransactionCache();
				tx.update(oldItem);
				tx.commit();

				LogPart lp = new LogPart();
				lp.setOpObjType(SOrg.class.getName());
				lp.setOpObjId(item.getUuid());
				lp.setRelObjType(SOrg.class.getName());
				lp.setRelObjId(item.getUuid());
				lp.setOpType(LogConstant.LOG_TYPE_MODIFY);
				lp.setLogLevel(LogConstant.LOG_LEVEL_COMMON);
				lp.setLogData(oldItem.toJson().toString());
				lp.setResult(LogConstant.RESULT_SUCCESS);
				lp.save();

				// 更新即时通讯缓存
				// OrganInfo orgItem = new OrganInfo();
				// orgItem.setId(item.getUuid());
				// orgItem.setName(item.getName());
				// IMCacheService.updateOrgInfo(orgItem);
			}
			tree.refresh();
			result = Ajax.JSONResult(Environment.RESULT_CODE_SUCCESS, Messages.getString("systemMsg.success"),
					oldItem.toJson());
			return Action.SUCCESS;
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
			if (tx != null) {
				tx.rollback();
			}
			result = Ajax.JSONResult(Environment.RESULT_CODE_ERROR, Messages.getString("systemMsg.exception"));
			return Action.ERROR;
		}
	}

	private boolean checkUpdate(SOrg oldItem, SOrg tmp) {
		if (tmp.getName().equals(oldItem.getName()) && tmp.getOpenFlag().equals(oldItem.getOpenFlag())
				&& tmp.getParentId().equals(oldItem.getParentId()) && tmp.getOrderNum().equals(oldItem.getOrderNum())
				&& tmp.getOrgType().equals(oldItem.getOrgType()) && tmp.getOrgLevel() == oldItem.getOrgLevel())
			return false;
		else {
			return true;
		}
	}

	@Override
	public Object getModel() {
		return item;
	}

	public SOrg getItem() {
		return item;
	}

	public void setItem(SOrg item) {
		this.item = item;
	}

}
